<?php
namespace OkayLicense;
use Okay\Core\Design;
use Okay\Core\OkayContainer\OkayContainer;
use Okay\Core\Router;
use Okay\Core\Modules\Module;
use Okay\Core\ServiceLocator;
use Smarty;
class License
{
    private static $module;
    private static $smarty;
    private static $inited = false;
    private $plugins;
    private static $modulesRoutes = array();

    public static function getHtml(Design $design, $template)
    {
        if ($design->isUseModuleDir() === false)
        {
            $design->setSmartyTemplatesDir($design->getDefaultTemplatesDir());
        }
        else
        {
            $vendor = self::get_vendor($design->getModuleTemplatesDir());
            $module_name = self::get_name($design->getModuleTemplatesDir());
            $design->setSmartyTemplatesDir(array(
                rtrim($design->getDefaultTemplatesDir() , '/') . "/../modules/{$vendor}/{$module_name}/html",
                $design->getModuleTemplatesDir() ,
                $design->getDefaultTemplatesDir()
            ));
        }
        $html = self::$smarty->fetch($template);
        return $html;
    }
    private static function get_vendor($module)
    {
        $module = str_replace(DIRECTORY_SEPARATOR, '/', $module);
        return preg_replace('~.*/?Okay/Modules/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?.*~', '$1', $module);
    }
    private static function get_name($module)
    {
        $module = str_replace(DIRECTORY_SEPARATOR, '/', $module);
        return preg_replace('~.*/?Okay/Modules/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?.*~', '$2', $module);
    }
    public function startModule($id, $vendor, $module_name)
    {
        if (empty(self::$module))
        {
            return array();
        }
        $container = OkayContainer::getInstance();
        $startmodule = array();
        $initclass = self::$module->getInitClassName($vendor, $module_name);
        if (!empty($initclass))
        {
            $mod = new $initclass((int)$id, $vendor, $module_name);
            $mod->init();
            foreach ($mod->getBackendControllers() as $start)
            {
                $start = $vendor . '.' . $module_name . '.' . $start;
                if (!in_array($start, $startmodule))
                {
                    $startmodule[] = $start;
                }
            }
        }
        $routes = self::$module->getRoutes($vendor, $module_name);

        $services = self::$module->getServices($vendor, $module_name);
        $container->bindServices($services);
        $smartyplugins = self::$module->getSmartyPlugins($vendor, $module_name);
        $container->bindServices($smartyplugins);

        foreach ($smartyplugins as $key => $plugin)
        {
            $this->plugins[$key] = $plugin;
        }
        self::$modulesRoutes = array_merge(self::$modulesRoutes, $routes);
        return $startmodule;
    }
    public function bindModulesRoutes()
    {
        Router::bindRoutes(self::$modulesRoutes);
    }
    public function registerSmartyPlugins()
    {
        if (!empty($this->plugins))
        {
            $service = ServiceLocator::getInstance();
            $design = $service->getService(Design::class);
            $smartymodule = $service->getService(Module::class);
            foreach ($this->plugins as $plugin)
            {
                $svc = $service->getService($plugin['class']);
                $svc->register($design, $smartymodule);
            }
        }
    }
    public function check()
    {
        if (self::$inited === false)
        {
            $service = ServiceLocator::getInstance();
            self::$module = $service->getService(Module::class);
            self::$smarty = $service->getService(Smarty::class);

            self::$inited = true;
        }
        return true;
    }
}
